package cn.yj.frame.spring;

import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.BeanDefinitionHolder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

import java.util.Set;

/**
 * 注册
 * @author 永健
 * @since 2022-01-18 15:06
 */
public class YJRpcServiceScanRegistrar implements ImportBeanDefinitionRegistrar {

    /**
     * 扫描结果进行注册。
     * 接口暴露可在此处做
     * @param importingClassMetadata
     * @param registry
     */
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        Set<BeanDefinitionHolder> beanDefinitionHolders = new YJClassPathScanner(registry).doScan("cn.yj");
        beanDefinitionHolders.forEach(beanDefinitionHolder -> {
            registry.removeBeanDefinition(beanDefinitionHolder.getBeanName());
            BeanDefinition beanDefinition = beanDefinitionHolder.getBeanDefinition();
            registry.registerBeanDefinition(beanDefinitionHolder.getBeanName(),beanDefinition);
        });
    }
}
